package com.hancher.common.net.subscribe;

import com.hancher.common.androidutil.ToastUtil;

import org.json.JSONException;

import java.net.SocketTimeoutException;
import java.text.ParseException;

import io.reactivex.rxjava3.functions.Consumer;
import retrofit2.HttpException;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/8/5 9:27 <br/>
 * 描述 :
 */
public class ToastConsumer implements Consumer<Throwable> {
    @Override
    public void accept(Throwable throwable) {
        String msg;
        if (throwable instanceof HttpException) {
            msg = "服务器：" + (((HttpException) throwable).code());
        } else if (throwable instanceof SocketTimeoutException) {
            msg = "网络超时";
        } else if (throwable instanceof ParseException || throwable instanceof JSONException) {
            msg = "数据解析错误：" + throwable.getMessage();
        } else {
            msg = "网络错误" + throwable.getMessage();
        }
        ToastUtil.show(msg);
    }
}
